<?php
defined('BASEPATH') OR exit('No direct script access allowed');
require_once '/application/libraries/PHPExcel.php';

class Order extends CI_Controller {

    //构造函数
    public function __construct()
    {
		parent::__construct();
	    $this->load->model('Agent_m');
	    $this->load->model('Order_m');
	    $this->load->model('Config_m');
	    $this->load->model('Recharge_m');
	    $this->load->model('Device_m');
	}

	/*订单列表*/
	public function orderList()
	{
		//订单号，用户，时间查询
		if(isset($_POST['key'])){
			$key = trim($_POST['key']);
			$con = "and (a.order_on like '%{$key}%' or a.endtime like '%{$key}%')";
			$this->db->like('order_on', $key);
			$this->db->or_like('endtime', $key);
		}else{
			//订单状态查询
			if(isset($_POST['paystate'])){
				if($_POST['paystate'] == "1"){
					$con = "and a.paystate = 1";
			        $this->db->where('paystate', 1);
				}elseif($_POST['paystate'] == "0"){
					$con = "and a.paystate = 0";
					 $this->db->where('paystate', 0);
				}else{
					$con = "";
				}
			}else{
				$con = "";
			}
		}
	    $total = $this->db->count_all_results('order');

		$page = $_POST['page'];
        $limit = $_POST['limit'];
        $offset = ($page - 1) * $limit;
        $sql = "select a.*,b.nickname from  mifi_order as a, mifi_users as b where a.user_id = b.user_id $con order by orderid desc limit ".$offset.",".$limit."";
        $order = $this->db->query($sql)->result_array();
        if($order){
	        foreach ($order as $key => $val) {
	        	//查询套餐
				if ($val['goods_on']) {
					$taos = $this->Recharge_m->get_one(array('package_id'=>$val['goods_on']));
					$order[$key]['taocan'] = $taos['flow'];
				}else{
					$order[$key]['taocan'] = '';
				}

	        	//查询代理商
				$device = $this->Device_m->get_ones(array('device_sn'=>$val['device_id']));
				$oneDaili = $this->Agent_m->get_one(array('id'=>$device['agent1']));
				$twoDaili = $this->Agent_m->get_one(array('id'=>$device['agent2']));
				if ($oneDaili['agent_name']) {
					$order[$key]['oneDaili'] = $oneDaili['agent_name'];
				}else{
					$order[$key]['oneDaili'] = '';
				}
				if ($twoDaili['agent_name']) {
					$order[$key]['twoDaili'] = $twoDaili['agent_name'];
				}else{
					$order[$key]['twoDaili'] = '';
				}
			}
       	}
	    echo json_encode(['code'=>0,'count'=>$total,'data'=>$order]);
	}
	//订单详情
	public function order_details(){
		$orderid = $_POST['orderid'];
		$sql = "select a.*,b.nickname from  mifi_order as a, mifi_users as b where orderid=".$orderid." and a.user_id = b.user_id";
        $order = $this->db->query($sql)->result_array();
        if($order){
	        foreach ($order as $key => $val) {
	        	//查询套餐
				if ($val['goods_on']) {
					$taos = $this->Recharge_m->get_one(array('package_id'=>$val['goods_on']));
					$order[$key]['taocan'] = $taos['flow'];
				}else{
					$order[$key]['taocan'] = '';
				}

	        	//查询代理商
				$device = $this->Device_m->get_ones(array('device_sn'=>$val['device_id']));
				$oneDaili = $this->Agent_m->get_one(array('id'=>$device['agent1']));
				$twoDaili = $this->Agent_m->get_one(array('id'=>$device['agent2']));
				$sanDaili = $this->Agent_m->get_one(array('id'=>$device['agent3']));
				$siDaili = $this->Agent_m->get_one(array('id'=>$device['agent4']));
				if ($oneDaili['agent_name']) {
					$order[$key]['oneDaili'] = $oneDaili['agent_name'];
					$order[$key]['yongjin1'] = $device['yongjin1'];
				}else{
					$order[$key]['oneDaili'] = '';
					$order[$key]['yongjin1'] = '';
				}
				if ($twoDaili['agent_name']) {
					$order[$key]['twoDaili'] = $twoDaili['agent_name'];
					$order[$key]['yongjin2'] = $device['yongjin2'];
				}else{
					$order[$key]['twoDaili'] = '';
					$order[$key]['yongjin2'] = '';
				}
				if ($sanDaili['agent_name']) {
					$order[$key]['sanDaili'] = $sanDaili['agent_name'];
					$order[$key]['yongjin3'] = $device['yongjin3'];
				}else{
					$order[$key]['sanDaili'] = '';
					$order[$key]['yongjin3'] = '';
				}
				if ($siDaili['agent_name']) {
					$order[$key]['siDaili'] = $siDaili['agent_name'];
					$order[$key]['yongjin4'] = $device['yongjin4'];
				}else{
					$order[$key]['siDaili'] = '';
					$order[$key]['yongjin4'] = '';
				}
			}
       	}
	    echo json_encode(['code'=>0,'count'=>1,'data'=>$order]);
	}
	/*导出订单*/
	public function exportorder()
	{
		/*设置head头信息*/
	    Header("Content-Type:application/vnd.ms-excel;charset=UTF-8");
	    Header("Accept-Ranges:bytes");
	    Header("Content-Disposition:attachment;filename=".date('YmdHis').".xls");
	    Header("Pragma:no-cache");
	    Header("Expires:0");

	    $str = $str2 = null;
	    /*设置表格信息*/
	    $str.= "编号"."\t";
	    $str.= "订单号"."\t";
	    $str.= "用户"."\t";
	    $str.= "一级代理商"."\t";
	    $str.= "二级代理商"."\t";
	    $str.= "设备号"."\t";
	    $str.= "套餐"."\t";
	    $str.= "充值金额(元)"."\t";
	    $str.= "结束时间"."\t";
	    $str.= "一级佣金比例"."\t";
	    $str.= "二级佣金比例"."\t";
	    $str.= "支付状态"."\t\r\n";
	    $info = iconv("UTF-8","GBK",$str);
	    echo $str;

		$sql = "select a.*,b.nickname from  mifi_order as a, mifi_users as b where a.user_id = b.user_id order by orderid desc";
        $order = $this->db->query($sql)->result_array();
        if($order){
	        foreach ($order as $key => $val) {
	        	//支付状态
	        	if($val['paystate'] == 1){
                    $order[$key]['paystate'] = '已支付';
	        	}else{
                    $order[$key]['paystate'] = '未支付';
	        	}

	        	//查询套餐
				if ($val['goods_on']) {
					$taos = $this->Recharge_m->get_one(array('package_id'=>$val['goods_on']));
					$order[$key]['taocan'] = $taos['flow'];
				}else{
					$order[$key]['taocan'] = '';
				}

	        	//查询代理商
				$device = $this->Device_m->get_ones(array('device_sn'=>$val['device_id']));
				$oneDaili = $this->Agent_m->get_one(array('id'=>$device['agent1']));
				$twoDaili = $this->Agent_m->get_one(array('id'=>$device['agent2']));
				if ($oneDaili['agent_name']) {
					$order[$key]['oneDaili'] = $oneDaili['agent_name'];
				}else{
					$order[$key]['oneDaili'] = '';
				}
				if ($twoDaili['agent_name']) {
					$order[$key]['twoDaili'] = $twoDaili['agent_name'];
				}else{
					$order[$key]['twoDaili'] = '';
				}

				$str2.= $val['orderid']."\t";
				$str2.= $val['order_on']."\t";
				$str2.= $val['nickname']."\t";
				$str2.= $order[$key]['oneDaili']."\t";
				$str2.= $order[$key]['twoDaili']."\t";
				$str2.= $val['device_id']."\t";
				$str2.= $order[$key]['taocan']."\t";
				$str2.= $val['order_money']."\t";
				$str2.= $val['endtime']."\t";
				$str2.= $val['yongjin_one']."\t";
				$str2.= $val['yongjin_two']."\t";
		        $str2.= $order[$key]['paystate']."\t\r\n";
                $info = iconv("UTF-8","GBK",$str2);
			}
       	}
       	echo $str2;
	}

	//单条记录删除订单列表
	public function delorder()
	{
		$orderid = $this->input->post('orderid',true);
		$this->db->where('orderid', $orderid);
        $del = $this->db->delete('order'); 
        if($del){
           echo json_encode(['code'=>1,'infomsg'=>"删除成功"]);
        }else{
           echo json_encode(['code'=>0,'infomsg'=>"删除失败"]);
        }
	}

	//批量删除订单记录
	public function delallorder()
	{
		// $orderid = json_decode($this->input->post('orderid',true));
		$orderid = $this->input->post('orderid',true);
		$this->db->where_in('orderid',$orderid);
        $delall = $this->db->delete('order');
        if($delall){ 
           echo json_encode(['code'=>1,'infomsg'=>"删除成功"]);	
        }else{
           echo json_encode(['code'=>0,'infomsg'=>"删除失败"]);
        }
	}

  
}
